import { createRouter, createWebHashHistory } from "vue-router";
import HomeView from "../views/home/HomeView.vue";
import favors from "../components/faovr/favors.vue";
import history from "../components/faovr/history.vue";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      // name: 'home',
      redirect: "/home",
      component: HomeView,
    },
    {
      path: "/home",
      name: "home",
      // redirect:'/home',
      component: HomeView,
    },
    {
      path: "/favor",
      name: "favor",
      redirect: "/favors",
      children: [
        {
          path: "/favors",
          name: "favors",
          component: favors,
        },
        {
          path: "/history",
          name: "history",
          component: history,
        },
      ],
      component: () => import("../views/favor/FavorView.vue"),
    },
    {
      path: "/message",
      name: "message",
      component: () => import("../views/message/MessageView.vue"),
      meta: {},
    },
    {
      path: "/order",
      name: "order",
      redirect: "/notoder",
      component: () => import("../views/order/OrderView.vue"),
      children: [
        {
          path: "/alloder",
          name: "alloder",
          component: () => import("../components/oder/AllOder.vue"),
        },
        {
          path: "/notoder",
          name: "notoder",
          component: () => import("../components/oder/NotOder.vue"),
        },
      ],
    },
    {
      path: "/city",
      name: "city",
      component: () => import("../views/home/CityView.vue"),
    },
    {
      path: "/search",
      name: "search",
      component: () => import("../views/search/SearchView.vue"),
    },
    {
      path: "/detail/:id",
      name: "detail",
      component: () => import("../views/detail/DetailView.vue"),
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login/LoginView.vue"),
    },
  ],
});

//验证登录
//设置路由导航守卫
router.beforeEach((to, from, next) => {
  const account = localStorage.getItem("account");
  if (to.path !== "/home") {
    if (account != null) {
      console.log("已登录可以进入");
      next(); // 放行
    } else {
      if (to.path !== "/login") {
        // 检查是否已经在登录页面
        console.log("去登录");
        next("/login"); // 导航到登录页面
      } else {
        next(); // 放行登录页面
      }
    }
  } else {
    next(); // 放行其他页面
  }
});

export default router;
